﻿using Red.Controls.Shell;
using Red.Toolkit.MeshEditor.ViewModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Red.Toolkit.MeshEditor.Controls
{
    /// <summary>
    /// Interaction logic for LodsGeneratorDialog.xaml
    /// </summary>
    public partial class LodsGeneratorDialog : RedDialog
    {
        #region Data

        private ObservableCollection<LodDescriptionViewModel> _lodDescriptions = new ObservableCollection<LodDescriptionViewModel>();

        #endregion

        #region Properties

        public List<LodDescriptionViewModel> LodDescriptions
        {
            get { return _lodDescriptions.ToList(); }
        }

        #endregion

        #region C-tor

        public LodsGeneratorDialog( FrameworkElement ownerSource, List<LodDescriptionViewModel> lodList = null )
            : base( ownerSource )
        {
            Type = RedDialogType.OkCancel;
            if ( lodList != null )
            {
                _lodDescriptions = new ObservableCollection<LodDescriptionViewModel>( lodList );
            }

            InitializeComponent();

            _lodsDescriptionsList.ItemsSource = _lodDescriptions;
        }

        #endregion

        #region Public methods

        public bool Execute()
        {
            return Show();
        }

        #endregion

        #region Event handlers

        private void AddLodButton_Click( object sender, RoutedEventArgs e )
        {
            AddLodDialog addLodDlg = new AddLodDialog( Window.GetWindow( this ) );
            if ( addLodDlg.Execute_DataMode() )
            {
                _lodDescriptions.Add( new LodDescriptionViewModel( addLodDlg.LodDesc.ReductionFactor, addLodDlg.LodDesc.Distance, addLodDlg.LodDesc.ChunksLimit ) );
            }
        }

        private void EditLodButton_Click( object sender, RoutedEventArgs e )
        {
            LodDescriptionViewModel selectedLod = _lodsDescriptionsList.SelectedItem as LodDescriptionViewModel;
            if ( selectedLod != null )
            {
                AddLodDialog addLodDlg = new AddLodDialog( Window.GetWindow( this ), new LodDescriptionViewModel( selectedLod ) );
                if ( addLodDlg.Execute_DataMode() )
                {
                    selectedLod = new LodDescriptionViewModel( addLodDlg.LodDesc );
                }
            }
        }

        private void RemoveLodButton_Click( object sender, RoutedEventArgs e )
        {
            LodDescriptionViewModel selectedLod = _lodsDescriptionsList.SelectedItem as LodDescriptionViewModel;
            if ( selectedLod != null )
            {
                _lodDescriptions.Remove( selectedLod );
            }
        }

        private void OkButton_Click( object sender, RoutedEventArgs e )
        {
            DialogResult = true;
        }

        private void LodsDescriptionsList_SelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            bool enablePerItemActions = _lodsDescriptionsList.SelectedItem != null;
            _editBtn.IsEnabled = enablePerItemActions;
            _removeBtn.IsEnabled = enablePerItemActions;
        }

        #endregion
    }
}
